package vip.xiaonuo.modular.tasks;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.log.Log;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import org.springframework.stereotype.Component;
import vip.xiaonuo.core.enums.CommonStatusEnum;
import vip.xiaonuo.core.timer.TimerTaskRunner;
import vip.xiaonuo.modular.xjbsysdetail.service.XjbSysdetailService;
import vip.xiaonuo.sys.modular.dict.entity.SysDictData;
import vip.xiaonuo.sys.modular.dict.entity.SysDictType;
import vip.xiaonuo.sys.modular.dict.param.SysDictDataParam;
import vip.xiaonuo.sys.modular.dict.service.SysDictDataService;
import vip.xiaonuo.sys.modular.dict.service.SysDictTypeService;
import vip.xiaonuo.sys.modular.org.service.SysOrgService;
import vip.xiaonuo.sys.modular.user.service.SysUserService;

import javax.annotation.Resource;
import java.util.List;

/**
 * 将表里的数据刷成字典
 */
@Component
public class DbDataTaskRunner implements TimerTaskRunner {

    private static final Log log = Log.get();

    @Resource
    private SysDictDataService sysDictDataService;
    @Resource
    private SysDictTypeService sysDictTypeService;
    @Resource
    private SysOrgService sysOrgService;
    @Resource
    private SysUserService sysUserService;
    @Resource
    private XjbSysdetailService xjbSysdetailService;

    @Override
    public void action() {
        xjb_dict_sql();
    }

    private void xjb_dict_sql() {
        SysDictType SysDictType = getSysDictTypeByCode("xjb_dict_sql");
        List<SysDictData> SysDictDataList1 = getDataListByDictTypeId(SysDictType.getId());
        for (SysDictData n : SysDictDataList1) {
            String code = n.getCode();
            String sql = n.getValue();
            SysDictType SysDictTypenew = getSysDictTypeByCode(code);
            if (ObjectUtil.isEmpty(SysDictTypenew)) {
                log.error("isEmpty:" + code);
                continue;
            }
            int sort = 1;
            List<SysDictData> SysDictDataList = getDataListByDictTypeId(SysDictTypenew.getId());
            List<Record> recordList = Db.find(sql);
            for (Record r : recordList) {
                String rcode = r.getStr("code");
                String rvalue = r.getStr("value");
                if (StrUtil.hasBlank(rcode, rvalue)) continue;
                SysDictDataParam SysDictDataParam = new SysDictDataParam();
                SysDictDataParam.setTypeId(SysDictTypenew.getId());
                SysDictDataParam.setCode(rcode);
                SysDictDataParam.setValue(rvalue);
                SysDictDataParam.setSort(100 * sort++);
                SysDictDataParam.setRemark(rvalue + "=" + rcode);
                SysDictDataParam.setStatus(CommonStatusEnum.ENABLE.getCode());//正常
                if (inSysDictData(SysDictDataParam, SysDictDataList)) {  // 已经存在的
                } else {
                    sysDictDataService.add(SysDictDataParam);
                }
            }
        }
    }

    public SysDictType getSysDictTypeByCode(String code) {
        LambdaQueryWrapper<SysDictType> queryWrapperSysDictType = new LambdaQueryWrapper<>();
        queryWrapperSysDictType.eq(SysDictType::getStatus, CommonStatusEnum.ENABLE.getCode());
        queryWrapperSysDictType.eq(SysDictType::getCode, code); //  SELECT * FROM sys_dict_type where code=''
        SysDictType SysDictType = sysDictTypeService.getBaseMapper().selectOne(queryWrapperSysDictType);
        return SysDictType;
    }

    public List<SysDictData> getDataListByDictTypeId(Long dictTypeId) {
        //构造查询条件
        LambdaQueryWrapper<SysDictData> queryWrapper = new LambdaQueryWrapper<SysDictData>();
        queryWrapper.eq(SysDictData::getTypeId, dictTypeId).ne(SysDictData::getStatus, CommonStatusEnum.DELETED.getCode());
        //根据排序升序排列，序号越小越在前
        queryWrapper.orderByAsc(SysDictData::getSort);
        //查询dictTypeId下所有的字典项
        List<SysDictData> results = sysDictDataService.list(queryWrapper);
        return results;
    }

//    private SysDictType get_SysDictType(String code) {
//        LambdaQueryWrapper<SysDictType> queryWrapperSysDictType = new LambdaQueryWrapper<>();
//        queryWrapperSysDictType.eq(SysDictType::getCode, code); //  SELECT * FROM sys_dict_type where code='xjb_sysdetail'
//        return sysDictTypeService.getBaseMapper().selectOne(queryWrapperSysDictType);
//    }

    private boolean inSysDictData(SysDictDataParam sysDictDataParam, List<SysDictData> sysDictDataList) {
        for (SysDictData n : sysDictDataList) {
            if (n.getTypeId().equals(sysDictDataParam.getTypeId()) && n.getCode().equals(sysDictDataParam.getCode())) {
                return true;
            }
        }
        return false;
    }
}
